package com.zhentao.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.common.Result;
import com.zhentao.common.ResultCodeEnum;
import com.zhentao.domain.SysPowerBattery;
import com.zhentao.domain.SysPowerCabinet;
import com.zhentao.domain.Area;
import com.zhentao.domain.City;
import com.zhentao.domain.Province;
import com.zhentao.domain.SysSite;
import com.zhentao.domain.UseBattery;
import com.zhentao.domain.dto.PageDto;
import com.zhentao.domain.vo.Map;
import com.zhentao.domain.vo.SiteData;
import com.zhentao.domain.vo.SiteDataVo;
import com.zhentao.domain.vo.SiteVo;
import com.zhentao.service.SysPowerBatteryService;
import com.zhentao.service.SysPowerCabinetService;
import com.zhentao.service.SysSiteService;
import com.zhentao.mapper.SysSiteMapper;
import com.zhentao.service.UseBatteryService;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
* @author 86133
* @description 针对表【sys_site】的数据库操作Service实现
* @createDate 2024-08-20 17:17:50
*/
@Service
public class SysSiteServiceImpl extends ServiceImpl<SysSiteMapper, SysSite>
    implements SysSiteService{
    @Autowired
    private StringRedisTemplate redisTemplate;
    /**
     * 获取所有站点的信息
     * @return
     */
    @Override
    public Result getData() {
        SiteVo siteVo = new SiteVo();
        List<SysSite> list = this.list();
        List<SiteData> siteData = new ArrayList<>();
        Map map1 = new Map();
        HashMap<String, Object> map = new HashMap<>();
        for (SysSite sysSite : list) {
            SiteData data = new SiteData();
            data.setName(sysSite.getAddress());
            data.setValue(sysSite.getId()+100);
            siteData.add(data);
            List<BigDecimal> bigList=new ArrayList<>();
            bigList.add(BigDecimal.valueOf(sysSite.getX()));
            bigList.add(BigDecimal.valueOf(sysSite.getY()));
            map.put(sysSite.getAddress(),bigList);
        }
        map1.setData(map);
        siteVo.setData(siteData);
        siteVo.setMap(map1);
        return Result.build(siteVo, ResultCodeEnum.SUCCESS);
    }
    @Autowired
    private SysPowerCabinetService sysPowerCabinetService;
    @Autowired
    private SysPowerBatteryService sysPowerBatteryService;
    @Autowired
    private UseBatteryService useBatteryService;


    @Override
    public Result getByName(String name) {
        SysSite data = this.getOne(Wrappers.<SysSite>lambdaQuery().eq(SysSite::getAddress, name));
        List<UseBattery> useData = useBatteryService.list(Wrappers.<UseBattery>lambdaQuery().eq(UseBattery::getSid, data.getId()));
        ArrayList<SiteDataVo> siteDataVos = new ArrayList<>();
        if (data != null) {
            Integer max=0;
            List<SysPowerCabinet> list = sysPowerCabinetService.list(Wrappers.<SysPowerCabinet>lambdaQuery().eq(SysPowerCabinet::getSid, data.getId()));
            for (SysPowerCabinet sysPowerCabinet : list) {
                max+=sysPowerCabinet.getMaxNum();
                List<SysPowerBattery> numList = sysPowerBatteryService.list(Wrappers.<SysPowerBattery>lambdaQuery().eq(SysPowerBattery::getPcid, sysPowerCabinet.getId()));
            }
            siteDataVos.add(new SiteDataVo("已经出租的电池",useData.size()));
            siteDataVos.add(new SiteDataVo("可用电池",max-useData.size()));
        }
        return Result.build(siteDataVos, ResultCodeEnum.SUCCESS);
    }

    @Override
    public Result<Page<SysSite>> getSitePage(PageDto pageDto) {
        String province = redisTemplate.opsForValue().get("province");
        String area = redisTemplate.opsForValue().get("area");
        String city = redisTemplate.opsForValue().get("city");
        List<Province> provinceList = JSON.parseArray(province, Province.class);
        List<Area> areaList = JSON.parseArray(area, Area.class);
        List<City> cityList = JSON.parseArray(city, City.class);
        Page<SysSite> page = new Page<>(pageDto.getPage(), pageDto.getSize());
        this.page(page,Wrappers.<SysSite>lambdaQuery().like(StringUtils.checkValNotNull(pageDto.getMap().get("likeAddress")),SysSite::getAddress,pageDto.getMap().get("likeAddress")));
        List<SysSite> records = page.getRecords();
        for (SysSite record : records) {
            provinceList.forEach(province1 -> {
                if (province1.getId().equals(record.getProvinceId())) {
                    record.setProvinceName(province1.getName());
                }
            });
            cityList.forEach(city1 -> {
                if (city1.getId().equals(record.getCityId())) {
                    record.setCityName(city1.getName());
                }
            });
            areaList.forEach(area1 -> {
                if (area1.getId().equals(record.getAreaId())) {
                    record.setAreaName(area1.getName());
                }
            });
        }

        return Result.build(page,200,"查询成功");
    }

    @Override
    public Result removeSite(Integer id) {
        boolean b = this.removeById(id);
        return b?Result.build(null,200,"删除成功"):Result.build(null,500,"删除失败");
    }

    @Override
    public Result SaveSite(SysSite sysSite) {
        sysSite.setCreateTime(new Date());
        sysSite.setImage("https://sghimages.shobserver.com/img/catch/2022/06/17/f6d762b0-9113-4b36-b977-abcc7a8f285b.jpg");
        boolean save = this.save(sysSite);
        return save?Result.build(null,200,"添加成功"):Result.build(null,500,"添加失败");
    }

    @Override
    public Result updateSite(SysSite sysSite) {
        boolean b = this.updateById(sysSite);
        return b?Result.build(null,200,"修改成功"):Result.build(null,500,"修改失败");
    }
}




